<?php

namespace app\common\model\xiluedu;

use think\Model;


class Category extends Model
{

    

    

    // 表名
    protected $name = 'xiluedu_category';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'default_status_text',
        'status_text'
    ];
    

    
    public function getDefaultStatusList()
    {
        return ['0' => __('Default_status 0'), '1' => __('Default_status 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getDefaultStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['default_status']) ? $data['default_status'] : '');
        $list = $this->getDefaultStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * 树状结构分类
     * @return bool|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function listTree(){
        return static::field('id,pid,name,thumb_image')
            ->with(['children'=>function($query){
                $query->where('status','1')
                    ->order('weigh','desc')
                    ->field('id,pid,name,thumb_image');
            }])
            ->where('status','1')
            ->where('pid',0)
            ->order('weigh','desc')
            ->select();
    }


    public static function lists($pid=0){
        return static::field('id,pid,name,thumb_image')
            ->where('status','1')
            ->where('pid',$pid)
            ->order('weigh','desc')
            ->select();
    }

    public function children(){
        return $this->hasMany('Category','pid','id');
    }

}
